#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2022/12/21 10:07
# @Author  : CHENWang
# @Site    : 
# @File    : whole_market_analysis.py
# @Software: PyCharm

"""
脚本说明:  全市场分析
"""

import os
import numpy as np
import pandas as pd
from quant_researcher.quant.datasource_fetch.crypto_api.self_defined import all_stalecoin_crypto
from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
from quant_researcher.quant.project_tool.localize import DATA_DIR
from quant_researcher.quant.project_tool.time_tool import calc_date_diff, get_yesterday
from TQR_Applications.factor_database.crypto.trading_data_weeks import special_max_drawdown_series, get_analysis_coin_list, data_reformat
from quant_researcher.quant.performance_attribution.core_functions.performance_analysis.performance import prices_cleaning
from quant_researcher.quant.project_tool import hammer


def get_all_data():
    """
    获取剔除稳定币之外的所有币的行情，交易额，市值数据

    :return:
    """

    # 把文件夹中各个小文件中的价格，市值，交易额数据汇总成一个文件
    # 如果之前已经读取过，不用重复读取，直接读取这些数据 all_prices_df, all_amount_df, all_marketcap_df
    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap')
    history_all_marketcap_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')
    history_date = history_all_marketcap_df.index[-1]
    temp_file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap\all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(temp_file_path, f'bitcoin')
    history_bitcoin_df = pd.read_excel(f'{temp_file_name}.xlsx', index_col='end_date')
    bitcoin_history_date = history_bitcoin_df.index[-1]
    if calc_date_diff(history_date, bitcoin_history_date) == 0:  # 说明all_prices_df， all_amount_df， all_marketcap_df已经读取汇总并保存过了，无需再保存
        file_name = os.path.join(file_path, f'all_history_ohlcvm_prices_raw')
        all_prices_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
        file_name = os.path.join(file_path, f'all_history_ohlcvm_amount_raw')
        all_amount_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
        file_name = os.path.join(file_path, f'all_history_ohlcvm_marketcap_raw')
        all_marketcap_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')
    else:
        print('开始加载全量历史数据-prices, amount, marketcap')
        all_prices_df, all_amount_df, all_marketcap_df = data_reformat()

    all_prices_df = all_prices_df.astype(np.float32)
    print('对价格数据做清洗')
    all_prices_df = all_prices_df.apply(prices_cleaning)
    all_prices_df.dropna(how='all', axis=1, inplace=True)

    # todo 统计各个币都是什么时候开始有行情的，可以类似于ICO的时间？？
    ico_date_series = all_prices_df.apply(lambda x: x[~x.isnull()].index[0])

    # 全市场总的交易金额, 用于后续计算每天最小交易额要求
    file_path_1 = os.path.join(DATA_DIR, 'trading_data')
    file_name_1 = os.path.join(file_path_1, f'cex_total_spot_amount_coinmarketcap')
    total_amount_series = pd.read_excel(f'{file_name_1}.xlsx', index_col='end_date')['amount']
    total_amount_series = total_amount_series.rolling(30).mean()  # 每天全市场交易额的30日均线

    # 截取需要分析的时间段,因为可能数据不是同一天获取的，分析截止日设置为最后一期币数大于8000个的日期【如果该日币数少于8000，则不分析】
    start_date = all_prices_df.index[0]
    analysis_end_date = all_prices_df.index[-1]
    coins_num_eachday = all_prices_df.count(axis=1)
    all_days = list(coins_num_eachday.index)
    all_days.sort(reverse=True)
    for day in all_days:
        if coins_num_eachday[day] > 8000:
            analysis_end_date = day
            break
        else:
            continue
    all_prices_df = all_prices_df.loc[start_date:analysis_end_date, ]
    all_amount_df = all_amount_df.loc[start_date:analysis_end_date, ]
    all_marketcap_df = all_marketcap_df.loc[start_date:analysis_end_date, ]
    total_amount_series = total_amount_series[start_date:analysis_end_date]

    # 剔除各种不需要分析的币，得到最终需要分析的币的名单
    all_coins_name_without_stablecoins = get_analysis_coin_list(all_prices_df)

    all_prices_df = all_prices_df[all_coins_name_without_stablecoins]
    all_amount_df = all_amount_df[all_coins_name_without_stablecoins]
    all_marketcap_df = all_marketcap_df[all_coins_name_without_stablecoins]

    return all_prices_df, all_amount_df, all_marketcap_df, total_amount_series


def cycle_top_bottom():
    """
    2017年 2021年顶部是否在抬升？2018年底部，2022年底部是否在抬升？

    :return:
    """
    all_prices_df, all_amount_df, all_marketcap_df, total_amount_series = get_all_data()
    # 2017-08-01  2018-08-01
    all_prices_df1 = all_prices_df.loc['2017-08-01':'2018-08-01', :]
    all_amount_df1 = all_amount_df.loc['2017-08-01':'2018-08-01', :]
    prices_top_2017 = all_prices_df1.max(axis=0)
    amount_mean_2017 = all_amount_df1.mean(axis=0)

    # 2020-10-01  2022-03-01
    all_prices_df2 = all_prices_df.loc['2020-10-01':'2022-03-01', :]
    prices_top_2021 = all_prices_df2.max(axis=0)

    # 2018-08-01  2019-06-01
    all_prices_df3 = all_prices_df.loc['2018-08-01':'2019-06-01', :]
    prices_bottom_2018 = all_prices_df3.min(axis=0)

    # 2020-02-01  2020-05-01
    all_prices_df4 = all_prices_df.loc['2020-02-01':'2020-05-01', :]
    prices_bottom_2020 = all_prices_df4.min(axis=0)

    # 2022-03-01  2022-12-01
    all_prices_df5 = all_prices_df.loc['2022-03-01':'2022-12-01', :]
    prices_bottom_2022 = all_prices_df5.min(axis=0)

    # 2021年顶部相比于2017年顶部涨跌幅
    top_ret = (prices_top_2021 - prices_top_2017) / prices_top_2017

    # 2022年底部相比于2018年底部涨跌幅
    bottom_ret = (prices_bottom_2022 - prices_bottom_2018) / prices_bottom_2018

    # 剔除掉交易额过小的
    top_ret_amount_constrain = top_ret[amount_mean_2017 >= 500000]
    bottom_ret_amount_constrain = bottom_ret[amount_mean_2017 >= 500000]

    # top_ret
    top_up_bottom_up_ratio = top_ret_amount_constrain[(top_ret_amount_constrain > 0) & (bottom_ret_amount_constrain > 0)].count() / top_ret_amount_constrain.count()
    top_down_bottom_down_ratio = top_ret_amount_constrain[(top_ret_amount_constrain < 0) & (bottom_ret_amount_constrain < 0)].count() / top_ret_amount_constrain.count()
    top_even_bottom_even_ratio = top_ret_amount_constrain[(abs(top_ret_amount_constrain) < 0.5) & (abs(bottom_ret_amount_constrain) < 0.5)].count() / top_ret_amount_constrain.count()


def new_19_low_analysis():
    """
    19年创18年新低占比，比18年低点高10%， 20%， 30%， 50%， 100%以上占比
    23年创22年底部新低，比22年底部高   以上占比

    :return:
    """
    all_prices_df, all_amount_df, all_marketcap_df, total_amount_series = get_all_data()

    all_marketcap_df2 = all_marketcap_df.loc['2017-12-01':'2018-01-01', :]
    # 2017年高点市值前50
    marketcap_top50_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2017年高点市值前100
    marketcap_top100_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2017年高点市值前200
    marketcap_top200_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    all_marketcap_df2 = all_marketcap_df.loc['2021-02-01':'2021-05-01', :]
    # 2021年高点市值前50
    marketcap_top50_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2021年高点市值前100
    marketcap_top100_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2021年高点市值前200
    marketcap_top200_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    # 18年低点
    all_prices_df2 = all_prices_df.loc['2018-06-01':'2019-06-01', :]
    all_prices_18_min = all_prices_df2.min()

    # 22年低点
    all_prices_df2 = all_prices_df.loc['2022-06-01':'2023-04-01', :]
    all_prices_22_min = all_prices_df2.min()

    all_result = []
    for top_num in [50, 100, 200]:
        lowret_ratio = pd.DataFrame([], columns=[f'{top_num}_>0', f'{top_num}_>0.1', f'{top_num}_>0.2', f'{top_num}_>0.3', f'{top_num}_>0.5', f'{top_num}_>1'],
                                    index=all_prices_df.loc['2018-01-01':, ].index)
        for date in all_prices_df.loc['2018-01-01':, ].index:
            if (date <= '2019-06-01') & (date >= '2018-01-01'):
                lowret_ratio.loc[date, :] = 0
            elif (date <= '2023-04-01') & (date >= '2020-05-01'):
                lowret_ratio.loc[date, :] = 0
            else:
                if (date > '2019-06-01') & (date < '2020-05-01'):
                    if top_num == 50:
                        coins_list = marketcap_top50_2017
                    elif top_num == 100:
                        coins_list = marketcap_top100_2017
                    elif top_num == 200:
                        coins_list = marketcap_top200_2017
                    min_series = all_prices_18_min
                elif date > '2023-04-01':
                    if top_num == 50:
                        coins_list = marketcap_top50_2021
                    elif top_num == 100:
                        coins_list = marketcap_top100_2021
                    elif top_num == 200:
                        coins_list = marketcap_top200_2021
                    min_series = all_prices_22_min
                else:
                    raise NotImplementedError

                low_ret = (all_prices_df.loc[date, coins_list] - min_series[coins_list]) / min_series[coins_list]
                for ratio_threshold in [0, 0.1, 0.2, 0.3, 0.5, 1]:
                    ratio = (low_ret[low_ret >= ratio_threshold]).count() / low_ret.count()
                    lowret_ratio.loc[date, f'{top_num}_>{ratio_threshold}'] = ratio
        all_result.append(lowret_ratio)
    all_result_df = pd.concat(all_result, axis=1)

    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2017-01-01', end_date=all_result_df.index[-1])
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_df = pd.concat([all_result_df, price_log_price['log_prices']], axis=1)
    all_df.sort_index(inplace=True)
    all_df = all_df.loc[all_result_df.index[0]:, :]

    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'all_lowret_ratio')
    all_df.to_csv(f'{file_name}.csv')


def drawdown_ratio():
    """
    跌幅超过95算破净，统计破净占比

    :return:
    """
    all_prices_df, all_amount_df, all_marketcap_df, total_amount_series = get_all_data()

    # 计算最大回撤数据， 批处理框架
    print('计算最大回撤')
    info = {'func': special_max_drawdown_series}
    list_for_test = [all_prices_df[coins] for coins in all_prices_df.columns]
    results = hammer.split_a_list_into_batches_and_deal_each_with_a_process(
        the_list=list_for_test, info=info, ignore_err=True, how_many=30, what_version='v2')
    all_maxdrawdown_df = pd.concat(results, axis=1)
    all_maxdrawdown_df = all_maxdrawdown_df[all_prices_df.columns]

    all_marketcap_df2 = all_marketcap_df.loc['2017-12-01':'2018-01-01', :]
    # 2017年高点市值前50
    marketcap_top50_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2017年高点市值前100
    marketcap_top100_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2017年高点市值前200
    marketcap_top200_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    all_marketcap_df2 = all_marketcap_df.loc['2021-02-01':'2021-05-01', :]
    # 2021年高点市值前50
    marketcap_top50_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2021年高点市值前100
    marketcap_top100_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2021年高点市值前200
    marketcap_top200_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    all_result = []
    for top_num in [50, 100, 200]:
        for ratio_threshold in [0.7, 0.8, 0.9, 0.95]:
            drawdawn_ratio = pd.Series()
            for date in all_prices_df.loc['2017-01-01':, ].index:
                if (date <= '2020-03-12'):
                    if top_num == 50:
                        coins_list = marketcap_top50_2017
                    elif top_num == 100:
                        coins_list = marketcap_top100_2017
                    elif top_num == 200:
                        coins_list = marketcap_top200_2017
                else:
                    if top_num == 50:
                        coins_list = marketcap_top50_2021
                    elif top_num == 100:
                        coins_list = marketcap_top100_2021
                    elif top_num == 200:
                        coins_list = marketcap_top200_2021
                temp_maxdrawdown_df = all_maxdrawdown_df.loc[date, coins_list]
                ratio = (temp_maxdrawdown_df[temp_maxdrawdown_df <= -ratio_threshold]).count() / temp_maxdrawdown_df.count()
                drawdawn_ratio[date] = ratio
                drawdawn_ratio.name = f'{top_num}_drawdawn{ratio_threshold}_ratio'
            all_result.append(drawdawn_ratio)
    all_result_df = pd.concat(all_result, axis=1)

    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2017-01-01', end_date=all_result_df.index[-1])
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_df = pd.concat([all_result_df, price_log_price['log_prices']], axis=1)
    all_df.sort_index(inplace=True)
    all_df = all_df.loc[all_result_df.index[0]:, :]

    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'all_drawdown_ratio')
    all_df.to_csv(f'{file_name}.csv')


def up_ratio():
    """
    计算1年内低点最大涨幅

    :return:
    """
    all_prices_df, all_amount_df, all_marketcap_df, total_amount_series = get_all_data()

    # 计算1年内低点最大涨幅
    all_upret_df = (all_prices_df - all_prices_df.rolling(365).min()) / all_prices_df.rolling(365).min()

    all_marketcap_df2 = all_marketcap_df.loc['2017-12-01':'2018-01-01', :]
    # 2017年高点市值前50
    marketcap_top50_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2017年高点市值前100
    marketcap_top100_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2017年高点市值前200
    marketcap_top200_2017 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    all_marketcap_df2 = all_marketcap_df.loc['2021-02-01':'2021-05-01', :]
    # 2021年高点市值前50
    marketcap_top50_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:50]
    # 2021年高点市值前100
    marketcap_top100_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:100]
    # 2021年高点市值前200
    marketcap_top200_2021 = all_marketcap_df2.mean(axis=0).sort_values(ascending=False).index[:200]

    all_result = []
    for top_num in [50, 100, 200]:
        for ratio_threshold in [3, 5, 10, 15, 20]:
            upret_ratio = pd.Series()
            for date in all_prices_df.loc['2017-01-01':, ].index:
                if (date <= '2020-03-12'):
                    if top_num == 50:
                        coins_list = marketcap_top50_2017
                    elif top_num == 100:
                        coins_list = marketcap_top100_2017
                    elif top_num == 200:
                        coins_list = marketcap_top200_2017
                else:
                    if top_num == 50:
                        coins_list = marketcap_top50_2021
                    elif top_num == 100:
                        coins_list = marketcap_top100_2021
                    elif top_num == 200:
                        coins_list = marketcap_top200_2021
                temp_upret_df = all_upret_df.loc[date, coins_list]
                ratio = (temp_upret_df[temp_upret_df >= ratio_threshold]).count() / temp_upret_df.count()
                upret_ratio[date] = ratio
                upret_ratio.name = f'{top_num}_upret{ratio_threshold}_ratio'
            all_result.append(upret_ratio)
    all_result_df = pd.concat(all_result, axis=1)

    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2017-01-01', end_date=all_result_df.index[-1])
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_df = pd.concat([all_result_df, price_log_price['log_prices']], axis=1)
    all_df.sort_index(inplace=True)
    all_df = all_df.loc[all_result_df.index[0]:, :]

    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'all_upret_ratio')
    all_df.to_csv(f'{file_name}.csv')


def marketcap_rotation():
    """
    市值轮动， 市值前200里面涨幅前50里面有多少是大市值的币？或者有多少是小市值币
    :return:
    """
    all_prices_df, all_amount_df, all_marketcap_df, total_amount_series = get_all_data()

    all_prices_df = all_prices_df.loc['2014-12-01':, :]
    all_amount_df = all_amount_df.loc['2015-01-01':, :]
    all_marketcap_df = all_marketcap_df.loc['2015-01-01':, :]

    all_weekly_ret_df = all_prices_df.pct_change(7)
    all_2weekly_ret_df = all_prices_df.pct_change(14)
    all_monthly_ret_df = all_prices_df.pct_change(30)

    all_result_df = pd.DataFrame(index=all_marketcap_df.index)
    for days in [7, 14, 30]:
        if days == 7:
            ret_df = all_weekly_ret_df.copy()
        elif days == 14:
            ret_df = all_2weekly_ret_df.copy()
        elif days == 30:
            ret_df = all_monthly_ret_df.copy()
        else:
            raise ValueError
        for marketcap_range in [100, 200, 300]:  # 统计范围
            for top_ret_range in [20, 30, 50]:  # 收益率前几
                for top_cap_range in [3, 5, 10, 20]:  # 收益率前几里面有多少是市值前多少内的
                    for date_index, row in all_marketcap_df.iterrows():
                        temp_marketcap_data = row.sort_values(ascending=False)
                        all_coins = temp_marketcap_data.index[:marketcap_range]
                        top_marketcap_coins = temp_marketcap_data.index[:top_cap_range]
                        temp_ret_data = ret_df.loc[date_index, all_coins]
                        temp_ret_data = temp_ret_data.sort_values(ascending=False)
                        top_ret_coins = temp_ret_data.index[:top_ret_range]

                        top_cap_in_top_ret = [i for i in top_marketcap_coins if i in top_ret_coins]
                        ratio = len(top_cap_in_top_ret)/top_cap_range

                        all_result_df.loc[date_index, f'{days}daysret-top{marketcap_range}capcoins-top{top_ret_range}ret-top{top_cap_range}cap_ratio'] = ratio

    # file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    # file_name = os.path.join(file_path, f'all_marketcap_rotation_ratio')
    # all_result_df = pd.read_csv(f'{file_name}.csv', index_col='end_date')

    all_columns = [i for i in all_result_df.columns if i != 'log_prices']
    for i in all_columns:
        all_result_df[i] = all_result_df[i].rolling(7).mean()

    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2015-01-01', end_date=all_result_df.index[-1])
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_df = pd.concat([all_result_df, price_log_price['log_prices']], axis=1)
    all_df.sort_index(inplace=True)
    all_df = all_df.loc[all_result_df.index[0]:, :]

    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'all_marketcap_rotation_ratio')
    all_df.to_csv(f'{file_name}.csv')


def btc_dominance_analysis(start_date=None, end_date=None):
    # BTC市值交易额占比分析， BTC加稳定币市值占比，BTC+ETH+稳定币市值占比，BTC+ETH交易额占比分析

    end_date = get_yesterday(marker='with_n_dash')  # 计算截止昨日收盘

    # 获取BTC价格数据
    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2015-01-01', end_date=end_date)
    price_log_price['log_prices'] = np.log10(price_log_price['close'])

    # 获取BTC，ETH市值交易额数据
    file_name_temp = os.path.join(DATA_DIR, f'BTC_history_ohlcvm')
    BTC_DATA = pd.read_excel(f'{file_name_temp}.xlsx', index_col='end_date')
    BTC_marketcap = BTC_DATA.loc['2015-01-01':, 'market_cap']
    BTC_marketcap.name = 'BTC_market_cap'
    BTC_amount = BTC_DATA.loc['2015-01-01':, 'amount']
    BTC_amount.name = 'BTC_amount'

    file_name_temp = os.path.join(DATA_DIR, f'ETH_history_ohlcvm')
    ETH_DATA = pd.read_excel(f'{file_name_temp}.xlsx', index_col='end_date')
    ETH_marketcap = ETH_DATA.loc['2015-01-01':, 'market_cap']
    ETH_marketcap.name = 'ETH_market_cap'
    ETH_amount = ETH_DATA.loc['2015-01-01':, 'amount']
    ETH_amount.name = 'ETH_amount'

    # 获取稳定币市值数据
    file_path_ssr = os.path.join(DATA_DIR, f'all_history_ohlcvm_coinmarketcap')
    file_name_ssr = os.path.join(file_path_ssr, f'stablecoin_coinmarketcap')
    stablecoin_marketcap = pd.read_excel(f'{file_name_ssr}.xlsx', sheet_name='stablecoin_marketcap', index_col='end_date')
    stablecoin_marketcap = stablecoin_marketcap.loc['2015-01-01':, 'stablecoin_market_cap']

    # 获取全市场市值数据
    file_path_temp = os.path.join(DATA_DIR, r'trading_data')
    file_name_temp = os.path.join(file_path_temp, f'cex_total_spot_amount_coinmarketcap')
    ALL_MARKET_DATA = pd.read_excel(f'{file_name_temp}.xlsx', index_col='end_date')
    ALL_marketcap = ALL_MARKET_DATA.loc['2015-01-01':, 'market_cap']
    ALL_marketcap.name = 'ALL_market_cap'
    ALL_amount = ALL_MARKET_DATA.loc['2015-01-01':, 'amount']
    ALL_amount.name = 'ALL_amount'

    all_market_cap = pd.concat([ALL_marketcap, stablecoin_marketcap, ETH_marketcap, BTC_marketcap, price_log_price['log_prices']], axis=1)
    all_market_cap.sort_index(inplace=True)
    all_amount = pd.concat([ALL_amount, ETH_amount, BTC_amount, price_log_price['log_prices']], axis=1)
    all_amount.sort_index(inplace=True)

    # for date in all_market_cap.index:
    #     temp_series = all_market_cap.loc[date, :]
    #     temp_series.sort_values(ascending=False, inplace=True)
    #     all_market_cap.loc[date, 'largest_marketcap'] = temp_series[0]
    #     all_market_cap.loc[date, 'largest2_marketcap'] = temp_series[:2].sum()
    #     all_market_cap.loc[date, 'largest3_marketcap'] = temp_series[:3].sum()
    #     all_market_cap.loc[date, 'largest5_marketcap'] = temp_series[:5].sum()
    #     all_market_cap.loc[date, 'largest10_marketcap'] = temp_series[:10].sum()
    #
    #     temp_series = all_market_cap.loc[date, :]
    #     temp_series.sort_values(ascending=False, inplace=True)
    #     all_market_cap.loc[date, 'largest_marketcap_without_stablecoins'] = temp_series[0]
    #     all_market_cap.loc[date, 'largest2_marketcap_without_stablecoins'] = temp_series[:2].sum()
    #     all_market_cap.loc[date, 'largest3_marketcap_without_stablecoins'] = temp_series[:3].sum()
    #     all_market_cap.loc[date, 'largest5_marketcap_without_stablecoins'] = temp_series[:5].sum()
    #     all_market_cap.loc[date, 'largest10_marketcap_without_stablecoins'] = temp_series[:10].sum()

    all_market_cap['Others_market_cap'] = all_market_cap['ALL_market_cap'] - (all_market_cap['BTC_market_cap'] + all_market_cap['ETH_market_cap'] + all_market_cap['stablecoin_market_cap'])
    all_market_cap['BTC_Dominance'] = all_market_cap['BTC_market_cap'] / all_market_cap['ALL_market_cap']
    all_market_cap['BTC+ETH_Dominance'] = (all_market_cap['BTC_market_cap'] + all_market_cap['ETH_market_cap']) / all_market_cap['ALL_market_cap']
    all_market_cap['BTC+stablecoins_Dominance'] = (all_market_cap['BTC_market_cap'] + all_market_cap['stablecoin_market_cap']) / all_market_cap['ALL_market_cap']
    all_market_cap['BTC+ETH+stablecoins_Dominance'] = (all_market_cap['BTC_market_cap'] + all_market_cap['ETH_market_cap'] + all_market_cap['stablecoin_market_cap']) / all_market_cap['ALL_market_cap']

    all_amount['Others_amount'] = all_amount['ALL_amount'] - (all_amount['BTC_amount'] + all_amount['ETH_amount'])
    all_amount['BTC_Dominance'] = all_amount['BTC_amount'] / all_amount['ALL_amount']
    all_amount['BTC+ETH_Dominance'] = (all_amount['BTC_amount'] + all_amount['ETH_amount']) / all_amount['ALL_amount']
    all_amount['Others_Dominance'] = all_amount['Others_amount'] / all_amount['ALL_amount']

    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 120).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 120).std()
    # all_market_cap['BTC Dominance Oscillator 120'] = (BTC_Dominance_Oscillator - ma) / std
    #
    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 140).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 140).std()
    # all_market_cap['BTC Dominance Oscillator 140'] = (BTC_Dominance_Oscillator - ma) / std
    #
    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 200).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 200).std()
    # all_market_cap['BTC Dominance Oscillator 200'] = (BTC_Dominance_Oscillator - ma) / std
    #
    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 365).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 365).std()
    # all_market_cap['BTC Dominance Oscillator 365'] = (BTC_Dominance_Oscillator - ma) / std
    #
    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 500).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 500).std()
    # all_market_cap['BTC Dominance Oscillator 500'] = (BTC_Dominance_Oscillator - ma) / std
    #
    # ma = pd.Series.rolling(BTC_Dominance_Oscillator, 730).mean()
    # std = pd.Series.rolling(BTC_Dominance_Oscillator, 730).std()
    # all_market_cap['BTC Dominance Oscillator 730'] = (BTC_Dominance_Oscillator - ma) / std

    file_path = os.path.join(DATA_DIR, r'all_history_ohlcvm_coinmarketcap')
    file_name = os.path.join(file_path, f'Market Cap Dominance Analysis')
    all_market_cap.to_csv(f'{file_name}.csv')

    file_name = os.path.join(file_path, f'Amount Dominance Analysis')
    all_amount.to_csv(f'{file_name}.csv')


def amount_bigger_than_btc():
    file_path = os.path.join(DATA_DIR, 'all_history_ohlcvm_coinmarketcap')
    temp_file_name = os.path.join(file_path, f'all_history_ohlcvm_amount')
    history_all_amount_df = pd.read_csv(f'{temp_file_name}.csv', index_col='end_date')

    result_df = pd.DataFrame(index=history_all_amount_df.index)
    result_df['coin_num'] = (~ history_all_amount_df.isnull()).sum(axis=1)

    test_all_amount_df = history_all_amount_df.copy()
    for col in test_all_amount_df:
        if col != 'bitcoin':
            test_all_amount_df[col] = test_all_amount_df[col] - test_all_amount_df['bitcoin']
    result_df['coin_tradeamount_>btc_num'] = (test_all_amount_df > 0).sum(axis=1)

    test_all_amount_df = history_all_amount_df.copy()
    for col in test_all_amount_df:
        if col != 'bitcoin':
            test_all_amount_df[col] = test_all_amount_df[col] - test_all_amount_df['bitcoin']/2
    result_df['coin_tradeamount_>0.5btc_num'] = (test_all_amount_df > 0).sum(axis=1)

    result_df['coin_tradeamount_>btc_ratio'] = result_df['coin_tradeamount_>btc_num'] / result_df['coin_num']
    result_df['coin_tradeamount_>0.5btc_ratio'] = result_df['coin_tradeamount_>0.5btc_num'] / result_df['coin_num']

    file_name = os.path.join(f'E:\\指标数据', f'BTC_history_ohlcvm')
    prices_df = pd.read_excel(f'{file_name}.xlsx', index_col='end_date')
    prices_df['log_price'] = np.log10(prices_df['close'])
    prices_df = prices_df.loc[result_df.index, 'log_price']
    result_df = pd.concat([result_df, prices_df], axis=1)
    file_name = os.path.join(f'E:\\指标数据', f'coinmarketcap_all_spot_usdt_amount_data')
    result_df.to_csv(f'{file_name}.csv')


if __name__ == '__main__':
    # get_all_data()
    # cycle_top_bottom()

    # drawdown_ratio()
    # up_ratio()

    # new_19_low_analysis()
    # marketcap_rotation()

    # BTC市值占比分析
    btc_dominance_analysis()

    # 交易额大于BTC的币分析
    # amount_bigger_than_btc()